﻿using System;
using System.Collections;
using System.Collections.Generic;

public class FunctionParameter: IEnumerable<Parameter>
{
    Parameter[] parameters;
    public Parameter this[int index]
    {
        get { return parameters[index]; }
    }
    public int Count
    {
        get { return parameters.Length; }
    }

    public static FunctionParameter Create(string arg)
    {
        return new FunctionParameter(arg);
    }

    public FunctionParameter(string arg)
    {
        parameters =GetParameters(arg);
    }

    Parameter[] GetParameters(string arg)
    {
        var array = arg.Split('~');
        Parameter[] result = new Parameter[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            var item = array[i].Split('#');
            result[i] = new Parameter(GetType(item[0]),item[1]);
        }
        return result;
    }


    Type GetType(string value)
    {
        Type result = null;
        foreach (var Assemblie in AppDomain.CurrentDomain.GetAssemblies())
        {
            var type = Assemblie.GetType(value, false, false);
            if (type != null)
            {
                result = type;
                break;
            }
        }
        return result;
    }

    public IEnumerator<Parameter> GetEnumerator()
    {
        for (int i = 0, end = parameters.Length; i < end; i++)
        {
            yield return parameters[i];
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}